{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8cddcdc1",
   "metadata": {},
   "source": [
    "# ipygany: Interactive library for 3-D mesh analysis\n",
    "\n",
    "## A Jupyter - ParaView bridge\n",
    "\n",
    "## https://github.com/QuantStack/ipygany\n",
    "\n",
    "\n",
    "A widget enabling VTK loader, Structured and Unstructured grids, Animations, Point cloud visualization and a lot more for 3-D visualization\n",
    "\n",
    "- BSD-3-Clause License\n",
    "\n",
    "**Installation:**\n",
    "\n",
    "```bash\n",
    "conda install -c conda-forge ipygany\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "671e1c2d",
   "metadata": {},
   "source": [
    "### Features\n",
    "\n",
    "* Warp\n",
    "* IsoColor\n",
    "* Threshold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f32f3ff1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Preparing example's data\n",
    "from pyvista import examples\n",
    "import numpy as np\n",
    "\n",
    "from ipywidgets import VBox, FloatSlider\n",
    "from ipygany import PolyMesh, Scene, IsoColor, WarpByScalar\n",
    "\n",
    "pvmesh = examples.download_topo_global()\n",
    "ugrid = pvmesh.cast_to_unstructured_grid()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8ca6782",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipygany import PolyMesh, Scene, IsoColor, WarpByScalar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "940efad0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Turn the PyVista mesh into a PolyMesh\n",
    "mesh = PolyMesh.from_vtk(ugrid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ab9fa2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Configure color mesh\n",
    "colored_mesh = IsoColor(mesh, min=-10421.0, max=6527.0)\n",
    "warped_mesh = WarpByScalar(colored_mesh, input='altitude', factor=0.5e-5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d14a7a21",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Link a slider to the warp value\n",
    "warp_slider = FloatSlider(min=0., max=5., value=0.5)\n",
    "\n",
    "def on_slider_change(change):\n",
    "    warped_mesh.factor = change['new'] * 1e-5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cba61a48",
   "metadata": {},
   "outputs": [],
   "source": [
    "warp_slider.observe(on_slider_change, 'value')\n",
    "\n",
    "VBox((warp_slider, Scene([warped_mesh])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28d8ee32",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipygany import ColorBar, colormaps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a66fd3e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "bar = ColorBar(colored_mesh)\n",
    "bar"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6caa5c3f",
   "metadata": {},
   "source": [
    "* Blues\n",
    "* Cividis\n",
    "* Greys\n",
    "* CubehelixDefault\n",
    "* Inferno"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
